<?php
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Trang đăng ký</title>
<script type="text/javascript">
	function CheckSignup()
	{
		var s=document. formdangky;
		if(s.hoten.value=='')
		{
			document.getElementById("hoten_error").style.display = "block";
			document.getElementById("hoten_error").innerHTML="Bạn chưa nhập họ tên!";
			s.hoten.focus();
			return false;	
		}
		else
			document.getElementById("hoten_error").style.display = "none";
			
		if(s.diachi.value=='')
		{
			document.getElementById("diachi_error").style.display = "block";
			document.getElementById("diachi_error").innerHTML=" Bạn chưa nhập địa chỉ!";
			s.diachi.focus();
			return false;	
		}
		else
			document.getElementById("diachi_error").style.display = "none";
			
		if(s.dienthoai.value=='')
		{
			document.getElementById("dienthoai_error").style.display= "block";
			document.getElementById("dienthoai_error").innerHTML=" Bạn chưa nhập điện thoại!";
			s.dienthoai.focus();
			return false;	
		}
		else
			document.getElementById("dienthoai_error").style.display = "none";
			
			
					if(isNaN(s.dienthoai.value))
		{
			document.getElementById("dienthoai_error").style.display= "block";
			document.getElementById("dienthoai_error").innerHTML="Điện thoại phải là số!";
			s.dienthoai.focus();
			return false;	
		}
		else
			document.getElementById("dienthoai_error").style.display = "none";
			
			
		
		if(s.email.value=='')
		{
			document.getElementById("email_error").style.display = "block";
			document.getElementById("email_error").innerHTML=" Bạn chưa nhập email!";
			s.email.focus();
			return false;	
		}
		else
		{
			var email = document.getElementById('email');
	 		 var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (!filter.test(email.value)) {
				document.getElementById("email_error").style.display = "block";
			    document.getElementById("email_error").innerHTML=" Bạn nhập email chưa đúng!";
			    s.email.focus();
				return false;
			 }
			else
			{
				document.getElementById("email_error").style.display = "none";	
			}
		}
			

			
if(s.user.value=='')
		{
			document.getElementById("user_error").style.display= "block";
			document.getElementById("user_error").innerHTML=" Bạn chưa nhập user!";
			s.user.focus();
			return false;	
		}
		else
			document.getElementById("user_error").style.display = "none";

	
		if(s.matkhau.value=='')
		{
			document.getElementById("matkhau_error").style.display= "block";
			document.getElementById("matkhau_error").innerHTML="Bạn chưa nhập mật khẩu!";
			s.matkhau.focus();
			return false;	
		}
		else
			document.getElementById("matkhau_error").style.display = "none";

if(s.xacnhanmatkhau.value=='')
		{
			document.getElementById("xacnhanmatkhau_error").style.display= "block";
			document.getElementById("xacnhanmatkhau_error").innerHTML="Bạn chưa nhập xác nhận mật khẩu!";
			s.xacnhanmatkhau.focus();
			return false;	
		}
		else
			document.getElementById("xacnhanmatkhau_error").style.display = "none";

		
		if(s.xacnhanmatkhau.value!=s.matkhau.value)
		{
			document.getElementById("xacnhanmatkhau_error").style.display= "block";
			document.getElementById("xacnhanmatkhau_error").innerHTML="mật khẩu không trùng!";
			s.xacnhanmatkhau.focus();
			return false;	
		}
		else
			document.getElementById("repeatpassword_error").style.display = "none";
		return true;	
	}
	
	
</script>

</head>

<body>
<?php
include("kn.php");
$hoten = $_POST["hoten"];
$phai = $_POST["phai"];
$diachi = $_POST["diachi"];
$ngaysinh = $_POST["nam"]."-".$_POST["thang"]."-".$_POST["ngay1"];
$dienthoai = $_POST["dienthoai"];
$email = $_POST["email"];
$user = $_POST["user"];
$matkhau = $_POST["matkhau"];
$xacnhanmatkhau = $_POST["xacnhanmatkhau"];
$lenh = "select * from khachhang where user='$user'";
$kq = mysql_query($lenh);
$n = mysql_num_rows($kq);
if($n >0  && $_POST["user"]!="")
{
?>
<script>
alert('Tài khoản này đã có!');
</script>
<?php	
}
else
{

if(isset($_POST["dangky"]))
{
	$lenhthem = "insert into khachhang(tenkhachhang,phai,diachi,dienthoai,user,matkhau,email,ngaysinh,xacnhanmatkhau) values('$hoten','$phai','$diachi','$dienthoai','$user','$matkhau','$email','$ngaysinh','$xacnhanmatkhau')";
	$kqthem = mysql_query($lenhthem);
	
	if($kqthem)
	{
		?>
		<script>
		alert('Đăng ký thành công!');
		
		</script>
		<?php
		 header("refresh:2,url=index.php");
		
		
		?>
		
	<?php	
	}
	else
	{
		
		?>
		<script>
		alert('Đăng ký không thành công!');
		
		</script>
		
		
	<?php	
	}
	
	
}

}

?>
<form id="formdangky" onSubmit="return CheckSignup()" name="formdangky" method="post" action="">
  <table width="550" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFF99" bordercolor="#FFCCCC" style="border-collapse:collapse">
    <tr>
      <td height="40" colspan="2" align="center" bgcolor="#FFFF66"><strong>ĐĂNG KÝ THÀNH VIÊN</strong></td>
    </tr>
    <tr>
      <td width="189" style="padding-left:15px">Họ tên</td>
      <td width="355"><label for="hoten"></label>
      <input name="hoten" type="text" id="hoten" size="40" value="<?php  echo $_POST["hoten"];  ?>" /><font color="#FF0000">(*)
      <span class="hide error" id="hoten_error"> </span> </font>

      
       </td>
    </tr>
    <tr>
      <td style="padding-left:15px">Phái</td>
      <td><label for="diachi"></label>
      <?php
	  if($_GET["phai"]==1)
        echo"<input type='radio' name='phai' id='nam' value='1'checked />
      <label for='nam'>Nam 
        <input type='radio' name='phai' id='nu' value='0' />
      Nữ ";
	  else
	   echo"<input type='radio' name='phai' id='nam' value='1'checked />
      <label for='nam'>Nam 
        <input type='radio' name='phai' id='nu' value='0' checked />
      Nữ ";
      
	  ?>
      </label></td>
    </tr>
    <tr>
      <td style="padding-left:15px">Địa chỉ</td>
      <td><label for="diachi"></label>
      <textarea name="diachi" cols="30" rows="3" id="diachi"><?php  echo $_POST["diachi"];  ?>
        </textarea>        <font color="#FF0000">(*)<span class="hide error" id="diachi_error"> </span></font></td>
    </tr>
    <tr>
      <td style="padding-left:15px">Ngày sinh</td>
      <td><label for="ngay">Ngày</label>
        <select name="ngay1" id="ngay1">
         <?php
		  for($i = 1; $i <= 31; $i++)
		  {
		  	if($i == date(d)	)  
		  echo"<option selected>$i</option>";
		  else
		   echo"<option>$i</option>";
		  }
		  ?>
       
      </select> <label for="thang">Tháng</label>
      <select name="thang" id="thang">
      <?php
		for($i = 1; $i <= 12; $i++)
		{
			if($i==date("m"))
			echo " <option selected>$i</option>";
			else
			echo " <option>$i</option>";
		}
		
		?>
      </select>
      <label for="nam">Năm</label>
      <select name="nam" id="nam">
      <?php
		for($i = 1975; $i <= 2015; $i++)
		{
			if($i==date("Y"))
			echo " <option selected>$i</option>";
			else
			echo " <option>$i</option>";
		}
		
		?>
      </select></td>
    </tr>
    <tr>
      <td style="padding-left:15px">Điện thoại</td>
      <td><label for="dienthoai"></label>
      <input name="dienthoai" type="text" id="dienthoai" size="40" value="<?php  echo $_POST["dienthoai"];  ?>"/><font color="#FF0000">(*)
      <span class="hide error" id="dienthoai_error"> </span> </font>
</td>
    </tr>
    <tr>
      <td style="padding-left:15px">Email</td>
      <td><label for="email"></label>
      <input name="email" type="text" id="email" size="40" value="<?php  echo $_POST["email"];  ?>"/><font color="#FF0000">(*)<span class="hide error" id="email_error"> </span></font></td>
    </tr>
    <tr>
      <td style="padding-left:15px">User</td>
      <td><label for="user"></label>
      <input name="user" type="text" id="user" size="40" value="<?php  echo $_POST["user"];  ?>"/><font color="#FF0000">(*)
      <span class="hide error" id="user_error"> </span> </font>
</td>
    </tr>
    <tr>
      <td style="padding-left:15px">Mật khẩu</td>
      <td><label for="matkhau"></label>
      <input name="matkhau" type="password" id="matkhau" size="40" value="<?php  echo $_POST["matkhau"];  ?>"/><font color="#FF0000">(*)
      <span class="hide error" id="matkhau_error"> </span> </font>
</td>
    </tr>
    <tr>
      <td style="padding-left:15px">Xác nhận mật khẩu</td>
      <td><label for="xacnhanmatkhau"></label>
        <input type="submit" name="dangky" id="dangky" value="Đăng ký" />
        <input name="xacnhanmatkhau" type="password" id="xacnhanmatkhau" size="40" value="<?php  echo $_POST["xacnhanmatkhau"];  ?>"/><font color="#FF0000">(*)
      <span class="hide error" id="xacnhanmatkhau_error"> </span> </font>
</td>
    </tr>
    <tr>
      <td height="35" colspan="2" align="center">&nbsp;</td>
    </tr>
    
  </table>
</form>
</body>
</html>